/*
 * asio.h
 *
 *  Created on: 2014. 6. 21.
 *      Author: jjaehuny
 */

#ifndef FRAMEWORK_ASIO_H_
#define FRAMEWORK_ASIO_H_

#include <thread>
#include <memory>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

#include <glog/logging.h>

#include "Util/StaticSingleton.h"
#include "Util/ThreadExecuter.h"

namespace Framework {

class asio : public Util::StaticSingleton<asio>
{
public:
	asio() : work_(io_service_)
	{
		LOG(INFO) << __PRETTY_FUNCTION__;
		this->run();
	}
	virtual ~asio() {}

	void run() {
		int workers = boost::thread::hardware_concurrency();
		LOG(INFO) << __PRETTY_FUNCTION__ << "[hardware_concurrency : " << workers << "]";

		threadExecuter_.Run(workers, boost::bind(& boost::asio::io_service::run, & io_service_));
	}

	boost::asio::io_service & io_service() { return io_service_; }

private:
	boost::asio::io_service io_service_;
	boost::asio::io_service::work work_;

	Util::ThreadExecuter threadExecuter_;
};

} /*Framework*/

#endif /* FRAMEWORK_ASIO_H_ */
